/*
Copyright [2012] [Maria Eugenia Malagamba]
Copyright [2012] [Leandro Pellegrino]
Copyright [2012] [Julia Gambuzzi]
Copyright [2012] [Ignacio Capodanno]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "TextBox.h"

TextBox::TextBox(int x, int y, string texto, int maximosCaracteres)
{	
	this->texto = texto;
	this->fuente =  TTF_OpenFont( "ARIAL.ttf", 18 );
	
	//Color de la fuente
	this->color.b = 0x00;
	this->color.g = 0x00;
	this->color.r = 0x00;

	this->imagenTexto = NULL;
	this->fondo = SDL_LoadBMP("textbox.bmp");

	this->atributos.x = x;
	this->atributos.y = y;
	this->atributos.w = this->fondo->w;
	this->atributos.h = this->fondo->h;
	this->seleccionada = false;
	this->maximosCaracteres = maximosCaracteres;
}

bool TextBox::ingreso(SDL_Event &evento){
	
	// Me voy si no se apretó el teclado
	if(evento.type != SDL_KEYDOWN)
		return false;

    // Dejo ingresar espacios, puntos, números o letras
	bool esEspacioNumeroOLetra = (evento.key.keysym.unicode == (Uint16)' ' || evento.key.keysym.unicode == (Uint16)'.')
								  || (evento.key.keysym.unicode >= (Uint16)'0' ) && ( evento.key.keysym.unicode <= (Uint16)'9')
								  || (evento.key.keysym.unicode >= (Uint16)'A' ) && ( evento.key.keysym.unicode <= (Uint16)'Z')
								  || (evento.key.keysym.unicode >= (Uint16)'a' ) && ( evento.key.keysym.unicode <= (Uint16)'z');
    
	if(esEspacioNumeroOLetra && this->texto.size() < this->maximosCaracteres)
    {
        //Append the character
        this->texto += (char)evento.key.keysym.unicode;
		return true;
    }
    
	// Si tocó backspace, remuevo un caracter del final
	if((evento.key.keysym.sym == SDLK_BACKSPACE ) && (this->texto.length() != 0 ) )
    {
        texto.erase( texto.length() - 1 );
		return true;
    }
}

void TextBox::mostrar(SDL_Surface* pantalla){

	// borro la imagen del texto anterior, si hay
	if (this->imagenTexto != NULL){
		SDL_FreeSurface(this->imagenTexto);
		SDL_FreeSurface(this->fondo);
		this->fondo = SDL_LoadBMP("textbox.bmp");
	}

	// si está clickeado le agrego el cursor |
	string textoAMostrar = this->texto;
	if (this->seleccionada) textoAMostrar += "|";
    
	// creo la imagen
	this->imagenTexto = TTF_RenderText_Solid(this->fuente, textoAMostrar.c_str(), this->color );

	// muestro la imagen
    if(this->imagenTexto != NULL)
    {
		SDL_Rect posicion;
		
		if (this->fondo->w - 5 > this->imagenTexto->w)
			posicion.x = 5;
		else
			posicion.x = this->fondo->w - this->imagenTexto->w - 5;

		posicion.y = ( this->fondo->h - this->imagenTexto->h ) / 2;
		SDL_BlitSurface(this->imagenTexto, NULL, this->fondo, &posicion); 
	
	}
	//El fondo lo saco del if xq no depende de si hay o no texto
	SDL_BlitSurface(this->fondo,NULL,pantalla,&atributos);
}

string TextBox::getValor(){
	return this->texto;
}

TextBox::~TextBox(){
	TTF_CloseFont( this->fuente);

	  //libero la superficie
    SDL_FreeSurface( this->imagenTexto );
    
    //Disable Unicode
    SDL_EnableUNICODE( SDL_DISABLE );  
}
